package meicai.appium.pages.control;

import org.testng.Assert;

import meicai.appium.pages.model.AllGoodsPage;
import meicai.appium.pages.model.GoodsInfoPage;
import meicai.appium.pages.model.OrderPage;
import meicai.appium.pages.model.ShoppingCartPage;

public class OrderControl extends PageBaseControl {

	public AllGoodsPage allGoods;
	public OrderPage orderPage;
	public GoodsInfoPage goodsInfoPage;
	public ShoppingCartPage shoppingCartPage;
	
	public OrderControl() {
		//logger.debug("ordercontrol");
		// TODO Auto-generated constructor stub
		allGoods = new AllGoodsPage();
		orderPage = new OrderPage();
		goodsInfoPage = new GoodsInfoPage();
		shoppingCartPage = new ShoppingCartPage();
	}

	
	/**
	 * 选择一级品类
	 * @param className
	 */
	public void selectFirstClass(String className) {
		
		logger.debug("选择一级品类   " + className);
		// 点击全部菜品
		if(engine.getDeviceName().equals("HUAWEI")) {
			engine.click(285, 1735); // click 全部菜品
		} else if (engine.getDeviceName().equals("oppo")) {
			//oppo
			engine.click(280,1800);// click 全部菜品
		} else if (engine.getDeviceName().equals("Sony")){
			engine.click(285, 1735);
			
		} else if (engine.getDeviceName().equals("Xiaomi")) {
			engine.click(330,1840);
		}else {
			logger.debug("i don't know which devices you are used.");
		}
		
		Assert.assertEquals(engine.isElementExist(allGoods.horizontalCategory),
				true, "horizontalScrollView don't exist");
		
		int tryTimes = 10;
		
		for(int i = 0; i <= tryTimes; i++) {
			if(engine.isContains(className)) {
				engine.clickElement(className);
				break;
			} else {				
				engine.swipeByXY(570, 520, 550, 520);	
				engine.sleep(2000);
				logger.debug(className +" is not exist " + i + "times");
				if(i == tryTimes) {
					Assert.fail("select" + className + " failed");
				}
			}
		}
		
		if(!engine.isContains(className)) {
			Assert.fail("select " + className + " failed");
		} 
	}
	
	/**
	 * 选择二级分类
	 */
	public void selectSecondClass(String secondClassName) {
			engine.clickElement(secondClassName);
	}
	
	/**
	 * 到商品详情页
	 * @param goodsName
	 */
	public void goToGoodsInfo(String goodsName) {
		logger.debug("去商品详情页  " + goodsName);
		int tryTimes = 6;
		for(int i = 0; i <= tryTimes; i++) {
			if(engine.isContains(goodsName)) {
				engine.clickElement(goodsName);
				break;
			} else {
				engine.swipeByXY(690,1400,690,1370);
				engine.sleep(5000);
				logger.debug(goodsName +" is not exist " + i + "times");
				if(i == tryTimes) {
					Assert.fail("select " + goodsName + " failed");
				}
			}
		}
		
		//加入常用清单提示
		if(engine.isElementExist(goodsInfoPage.tips)) {
			engine.click(600, 600);
		}
		
		if(!engine.isElementExist(goodsInfoPage.goodsName)) {
			Assert.fail(" go to " + goodsName + "'info page failed");
		}
	}
	
	/**
	 * 增加商品，且到购物车页
	 */
	public void addGoodsToShop() {
		logger.debug("增加商品，去购物车页");
		if(engine.getDeviceName().equals("HUAWEI")) {
			//华为mate9 不知道为什么得不到 "加入购物车"
			//click "加入购物车"
			engine.click(700, 1700);
			//click "购物车"
			engine.click(200, 1750);
			engine.sleep(2000);
	
		} else {
			//sony
			engine.clickElement(goodsInfoPage.addToCart);
			engine.clickElement(allGoods.add);
			
			engine.clickElement("购物车");
		}
		
//		if(!engine.isElementExist(shoppingCartPage.ssuPrice)) {
//			engine.screenShot("addGoodsToShop");
//			Assert.fail(" go to shoppingcart page failed");
//		}
	}
	
	/**
	 * 收银台页
	 * 收银台页时h5页面，单独写出来，以方便统一修改
	 */
	private void cashierPage() {
		logger.debug("点击   ‘确认支付’");
		if(engine.getDeviceName().equals("HUAWEI")) {
			//click 充值卡支付
			engine.click(300, 730);
			
			//click 确认支付
			engine.click(600, 1700);
			
		} else {//xiaomi
			//click 充值卡支付
			engine.click(540, 740);
			
			//click 确认支付
			engine.click(540, 1840);
			
		}
	}
	
	/**
	 * 结算-在线支付
	 */
	public void payOnLine() {
		logger.debug("点击   ‘去结算’");
		if(engine.getDeviceName().equals("HUAWEI")) {
			//click 去结算
			engine.click(900,1700);

		} else {			
			//click 去结算
			engine.clickElement(shoppingCartPage.useFreight);
		}
		logger.debug("点击   ‘继续购买’");
		if(engine.isElementExist(allGoods.continueBuy)) {
			engine.clickElement(allGoods.continueBuy);
		}
		engine.sleep(1 * 1000);
		logger.debug("点击   ‘在线支付’");
		engine.clickElement("在线支付");
		engine.sleep(3 * 1000);
		
		logger.debug("点击   ‘提交订单’");
		engine.clickElement("提交订单");
		
		engine.sleep(5 * 1000);
		
		cashierPage();
		
		if(!engine.isElementExist(orderPage.orderSucess)) {
			Assert.fail(" pay online failed");
		} else {
			logger.debug("pay online successfully");
		}
	}
	
	/**
	 * 结算-货到付款
	 */
	public void payOffLine() {
		logger.debug("点击   ‘去结算’");
		if(engine.getDeviceName().equals("HUAWEI")) {
			//click 去结算
			engine.click(900,1700);
		} else {
			//click 去结算
			engine.clickElement(shoppingCartPage.useFreight);
		}
		
		logger.debug("点击   ‘继续购买’");
		if(engine.isElementExist(allGoods.continueBuy)) {
			engine.clickElement(allGoods.continueBuy);
		}
		
		engine.sleep(1 * 1000);
		
		logger.debug("点击   ‘货到付款’");
		engine.clickElement("货到付款");
		
		engine.sleep(3 * 1000);
		
		logger.debug("点击   ‘提交订单’");
		engine.clickElement("提交订单");
		
		engine.sleep(3 * 1000);
		
		if(!engine.isElementExist(orderPage.orderSucess)) {
			Assert.fail(" pay offline failed");
		} else {
			logger.debug("pay offline successfully");
		}
	}
	
	/**
	 * 结算-在线支付-使用优惠券
	 */
	public void payOnLineWithCoupon() {
		logger.debug("点击   ‘去结算’");
		if(engine.getDeviceName().equals("HUAWEI")) {
			//click 去结算
			engine.click(900,1700);	
		} else {
			//click 去结算
			engine.clickElement(shoppingCartPage.useFreight);
		}
		
		logger.debug("点击   ‘继续购买’");
		if(engine.isElementExist(allGoods.continueBuy)) {
			engine.clickElement(allGoods.continueBuy);
		}
		
		logger.debug("点击   ‘在线支付’");
		engine.clickElement("在线支付");
		//上滑
		engine.swipe("up",5);
		
		//选择优惠券
		logger.debug("点击   ‘优惠券’");
		engine.clickElement(orderPage.useCoupon);
		
		engine.sleep(5 * 1000);
		logger.debug("点击   ‘选优惠券’");
		engine.clickElement(orderPage.selectCoupon);
		
		engine.sleep(3 * 1000);
		logger.debug("点击   ‘提交订单’");
		engine.clickElement("提交订单");
		
		engine.sleep(3 * 1000);
		
		cashierPage();
		
		if(!engine.isElementExist(orderPage.orderSucess)) {
			Assert.fail(" payOnLineWithCoupon failed");
		} else {
			logger.debug("payOnLineWithCoupon successfully");
		}
	}
	
	/**
	 * 结算-货到付款-使用优惠券
	 */
	public void payOffLineWithCoupon() {
		logger.debug("点击   ‘去结算’");
		if(engine.getDeviceName().equals("HUAWEI")) {
			//click 去结算
			engine.click(900,1700);			
		} else {
			//click 去结算
			engine.clickElement(shoppingCartPage.useFreight);
		}
		
		logger.debug("点击   ‘继续购买’");
		if(engine.isElementExist(allGoods.continueBuy)) {
			engine.clickElement(allGoods.continueBuy);
		}
		
		logger.debug("点击   ‘货到付款’");
		engine.clickElement("货到付款");
		
		//上滑
		engine.swipe("up",5);
		
		engine.sleep(1 * 1000);
		
		logger.debug("点击   ‘优惠券’");
		//选择优惠券
		engine.clickElement(orderPage.useCoupon);
		
		engine.sleep(5 * 1000);
		
		logger.debug("点击   ‘选优惠券’");
		engine.clickElement(orderPage.selectCoupon);
		
		engine.sleep(1 * 1000);
		
		logger.debug("点击   ‘提交订单’");
		engine.clickElement("提交订单");
		
		engine.sleep(3 * 1000);
		
		if(!engine.isElementExist(orderPage.orderSucess)) {
			Assert.fail(" payOffLineWithCoupon failed");
		} else {
			logger.debug("payOffLineWithCoupon successfully");
		}
	}
	
//	public void payOnlineWithFreightVoucher () {
//		
//	}
//	
//	public void payOffLineWithFreightVoucher () {
//		
//	}
}